package com.newtype.meeting.client.widget.backend;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.Title;
import com.newtype.meeting.shared.AccountSummaryDTO;

public class TitleEditWindow extends Window {
	private BeanModel titleBeanModel;
	private ListStore<AccountSummaryDTO> store = new ListStore<AccountSummaryDTO>();;

	public TitleEditWindow(Title title) {
		setHeading("编辑");
		setWidth(352);
		setAutoHeight(true);
		setModal(true);
		setButtonAlign(HorizontalAlignment.CENTER);

		FormPanel formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);

		LabelField degreeLabel = new LabelField();
		degreeLabel.setFieldLabel("职级:");
		degreeLabel.setName("degree");
		formPanel.add(degreeLabel);

		TextField<String> titleText = new TextField<String>();
		titleText.setFieldLabel("职称");
		titleText.setName("titleCaption");
		formPanel.add(titleText);

		titleBeanModel = BeanModelLookup.get().getFactory(Title.class)
				.createModel(title);
		//绑定数据
		FormBinding binding = new FormBinding(formPanel, true);
		binding.bind(titleBeanModel);

		add(formPanel);
		add(getAccountGrid());
		addButton(getSaveButton());
		addButton(getDeleteButton(title));
	}

	private Button getDeleteButton(final Title title) {
		final Listener<MessageBoxEvent> listener = new Listener<MessageBoxEvent>() {
			public void handleEvent(MessageBoxEvent ce) {
				Button btn = ce.getButtonClicked();
				if (btn.getText().equals("Yes")) {
					Dispatcher.forwardEvent(AppEvents.DeleteTitle, title);
					hide();
				}
			}
		};
		return new Button("删除", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (store.getCount() > 0) {
					MessageBox.alert("提示", "此职称尚在使用中，如果要删除此职称，请先变更使用者为其它职称!",
							null);
				} else {
					MessageBox.confirm("提示", "你确定要删除这笔数据吗? 删除的数据无法还原 !",
							listener);
				}
			}
		});
	}

	private Button getSaveButton() {
		return new Button("送出", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Title title = titleBeanModel.getBean();
				Dispatcher.forwardEvent(AppEvents.UpdateTitle, title);
				hide();
			}
		});
	}

	public ContentPanel getAccountGrid() {
		ContentPanel cp = new ContentPanel();
		cp.setBodyBorder(true);
		cp.setHeading("目前为此职称成员");
		cp.setLayout(new FitLayout());
		cp.setSize(340, 150);

		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		ColumnConfig column = new ColumnConfig();
		column.setId("fullName");
		column.setHeader("姓名");
		column.setWidth(100);
		column.setRowHeader(true);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("deptName");
		column.setHeader("部门");
		column.setWidth(170);
		configs.add(column);

		ColumnModel cm = new ColumnModel(configs);

		Grid<AccountSummaryDTO> grid = new Grid<AccountSummaryDTO>(store, cm);
		grid.setStyleAttribute("borderTop", "none");
		grid.setAutoExpandColumn("fullName");
		grid.setBorders(false);
		grid.setStripeRows(true);
		grid.setColumnLines(true);
		grid.setColumnReordering(true);
		grid.getAriaSupport().setLabelledBy(cp.getHeader().getId() + "-label");
		cp.add(grid);

		grid.setStateful(true);
		cp.add(grid);

		return cp;
	}

	public ListStore<AccountSummaryDTO> getStore() {
		return store;
	}

}
